package com.zimmor.gui;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 * @Author:zimmor
 * @Date: 2021/11/9 9:40
 * @Description
 * @version: 1.0.0
 */

public class GUIDemo1 {
    public static void main(String[] args) {
        GUIDemo1 guiDemo1 = new GUIDemo1();
        guiDemo1.demo4();

    }

    /**
     * 
     */
    public void demo5() {
        JFrame jFrame = new JFrame();
        jFrame.setVisible(true);
        jFrame.setSize(250, 220);
        jFrame.setVisible(true);
        jFrame.setLocation(400, 200);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel label1 = new JLabel("登陆界面");
        JLabel label2 = new JLabel("账号：");
        JLabel label3 = new JLabel("密码：");
        JTextField tf = new JTextField();
        JPasswordField psf = new JPasswordField();
        JRadioButton rb1 = new JRadioButton("记住密码");
        JRadioButton rb2 = new JRadioButton("自动登陆");
        JButton bt1 = new JButton("登陆");

        // 为指定的 Container 创建 GroupLayout
        GroupLayout layout = new GroupLayout(jFrame.getContentPane());
        jFrame.getContentPane().setLayout(layout);

        // 创建GroupLayout的水平连续组，，越先加入的ParallelGroup，优先级级别越高。
        GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
        // 添加间隔
        hGroup.addGap(20);
        hGroup.addGroup(layout.createParallelGroup().addComponent(label2).addComponent(label3));
        hGroup.addGap(20);
        hGroup.addGroup(layout.createParallelGroup().addComponent(label1).addComponent(psf).addComponent(rb1)
                .addComponent(rb2).addComponent(tf).addComponent(bt1));
        hGroup.addGap(20);
        // 设置水平分组
        layout.setHorizontalGroup(hGroup);

        // 创建GroupLayout的垂直连续组，，越先加入的ParallelGroup，优先级级别越高。
        GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
        vGroup.addGap(30);
        vGroup.addGroup(layout.createParallelGroup().addComponent(label1));
        vGroup.addGap(10);
        vGroup.addGroup(layout.createParallelGroup().addComponent(label2).addComponent(tf));
        vGroup.addGap(5);
        vGroup.addGroup(layout.createParallelGroup().addComponent(label3).addComponent(psf));
        vGroup.addGroup(layout.createParallelGroup().addComponent(rb1));
        vGroup.addGroup(layout.createParallelGroup().addComponent(rb2));
        vGroup.addGroup(layout.createParallelGroup().addComponent(bt1));
        vGroup.addGap(10);
        // 设置垂直组
        layout.setVerticalGroup(vGroup);
    }
    /**
     * 基础组件
     */
    public void demo4() {
        JFrame jFrame = new JFrame();
        jFrame.setSize(600,1000);
        jFrame.setLocation(200,200);
        List<JComponent> componentList = new ArrayList<>();
        //文本框组件
        JTextField jTextField = new JTextField("文本框",10);
        jTextField.setSize(10,5);
        componentList.add(jTextField);
        //密码框
        JPasswordField jPasswordField = new JPasswordField();
        componentList.add(jPasswordField);
        //标签
        JLabel jLabel = new JLabel("标签");
        componentList.add(jLabel);
        //按钮组件
        JButton jButton = new JButton("按钮");
        componentList.add(jButton);
        //复选框组件
        JCheckBox jCheckBox = new JCheckBox();
        componentList.add(jCheckBox);
        //单选框组件
        JRadioButton jRadioButton = new JRadioButton();
        componentList.add(jRadioButton);
        //下拉框组件
        Vector<String> vector = new Vector();
        vector.add("北京");
        vector.add("上海");
        vector.add("西安");
        JComboBox<String> objectJComboBox = new JComboBox<>(vector);
        componentList.add(objectJComboBox);
        //列表框
        JList<String> stringJList = new JList<>(vector);
        componentList.add(stringJList);
        //滚动窗格（列表框（JList）和滚动窗格（JScrollPane）一般会一起使用）
        //多行文本框
        //JTabbedPane：选项卡窗格（业签组件）。
        //JMenuBar：菜单条组件
        //JMenu：菜单组件
        //JMenuItem：菜单项组件
        //JToolBar:容器类组件
        for (JComponent jComponent : componentList) {
            jFrame.add(jComponent);
        }
        GridLayout gridLayout = new GridLayout(3,3,5,5);
        jFrame.setLayout(gridLayout);
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    /**
     * 网格布局
     */
    public void demo3() {
        JFrame jFrame = new JFrame();
        jFrame.setSize(600,1000);
        jFrame.setLocation(200,200);
        JButton jButton1 = new JButton("北北");
        JButton jButton2 = new JButton("南南");
        JButton jButton3 = new JButton("西西");
        JButton jButton4 = new JButton("东东");
        JButton jButton5 = new JButton("中中");
        JButton jButton6 = new JButton("东东");
        JButton jButton7 = new JButton("中中");
        GridLayout gridLayout = new GridLayout(3,3,5,5);
        jFrame.setLayout(gridLayout);
        jFrame.add(jButton1);
        jFrame.add(jButton2);
        jFrame.add(jButton3);
        jFrame.add(jButton4);
        jFrame.add(jButton5);
        jFrame.add(jButton6);
        jFrame.add(jButton7);

        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    /**
     * 流式布局
     */
    public void demo2() {
        JFrame jFrame = new JFrame();
        jFrame.setSize(300,500);
        jFrame.setLocation(200,200);
        JButton jButton1 = new JButton("北北");
        JButton jButton2 = new JButton("南南");
        JButton jButton3 = new JButton("西西");
        JButton jButton4 = new JButton("东东");
        JButton jButton5 = new JButton("中中");
        JButton jButton6 = new JButton("东东");
        JButton jButton7 = new JButton("中中");
        FlowLayout flowLayout = new FlowLayout();
        flowLayout.setAlignment(FlowLayout.LEFT);
        jFrame.setLayout(flowLayout);
        jFrame.add(jButton1);
        jFrame.add(jButton2);
        jFrame.add(jButton3);
        jFrame.add(jButton4);
        jFrame.add(jButton5);
        jFrame.add(jButton6);
        jFrame.add(jButton7);

        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    /**
     * 边界布局
     */
    public void demo1() {
        JFrame jFrame = new JFrame();
        jFrame.setSize(300,500);
        jFrame.setLocation(200,200);
        JPanel jPanel1 = new JPanel();
        JPanel jPanel2 = new JPanel();
        JButton jButton1 = new JButton("北北");
        JButton jButton2 = new JButton("南南");
        JButton jButton3 = new JButton("西西");
        JButton jButton4 = new JButton("东东");
        JButton jButton5 = new JButton("中中");
        jFrame.add(jButton1, BorderLayout.BEFORE_FIRST_LINE);
        jFrame.add(jButton2, BorderLayout.AFTER_LAST_LINE);
        jFrame.add(jButton3, BorderLayout.AFTER_LINE_ENDS);
        jFrame.add(jButton4, BorderLayout.BEFORE_LINE_BEGINS);
        jFrame.add(jButton5, BorderLayout.CENTER);

        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
